package com.hlcl.huaji.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.elcl.activity.fragment.BaseFragment;
import com.hlcl.huaji.R;
import com.hlcl.huaji.ctrl.adapter.OrderListPagerAdapter;
import com.hlcl.huaji.model.Confing;

/**
 * Created by lichunlin on 2018/5/17.
 * 首页的fragment
 */

public class HomeFragment extends BaseFragment {

    private View view;
    public static HomeFragment homeFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fra_home, null);

        homeFragment = this;
        initADView();
        initViewPager();
        return view;
    }

    private void initADView() {
        ImageView img_ad = view.findViewById(R.id.img_ad);
        Confing confing = ((HomeActivity) getActivity()).confing;
        if (null != confing){
            Glide.with(getActivity()).load(confing.getAd()).into(img_ad);
        }
    }

    public OrderListPagerAdapter pagerAdapter;

    private void initViewPager() {
        pagerAdapter = new OrderListPagerAdapter(getFragmentManager(), tabTitles,this);
        ViewPager viewPager = view.findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(5);
        viewPager.setAdapter(pagerAdapter);
        TabLayout tabLayout = view.findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < tabTitles.length; i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (null != tab) {
                tab.setCustomView(getTabView(i));
            }
        }
    }

    private String[] tabTitles = {"最新", "全部", "待处理", "处理中", "已完成"};

    public View getTabView(int position) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_tablayout_item, null);
        TextView tv_selector_title = v.findViewById(R.id.tv_selector_title);
        tv_selector_title.setText(tabTitles[position]);
        return v;
    }


    @Override
    protected void praseJson(int reqId, String json) {

    }
}
